//package mybatis.web;
//
//import java.io.File;
//import java.io.IOException;
//import java.util.List;
//
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import mybatis.entity.User;
//import mybatis.util.RandomId;
//import mybatis.service.UserService;
//import mybatis.service.impl.UserServiceImpl;
//import org.apache.commons.fileupload.FileItem;
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
//import org.apache.commons.fileupload.servlet.ServletFileUpload;
//
//
//
//@WebServlet("/upload/*")
//public class UploadServlet extends BaseServlet {
//	public void uploadHeader(HttpServletRequest request, HttpServletResponse response) {
//		User user =(User) request.getSession().getAttribute("user");
//		int userId = user.getId();
//		String randomStr = RandomId.RandomId(userId);
//		String path = request.getServletContext().getRealPath("/img/users/header");
//		File file = new File(path);
//		if(!file.exists()) file.mkdirs();
//		DiskFileItemFactory factory = new DiskFileItemFactory();
//		ServletFileUpload upload = new ServletFileUpload(factory);
//		try {
//			List<FileItem> list = upload.parseRequest(request);
//			for(FileItem fileItem : list) {
//				if(!fileItem.isFormField()) {
//					String name = fileItem.getName();
//					String[] names = name.split("\\.");
//					String newName = (name.split("\\."))[0]+"_"+randomStr+"."+names[1];
//					System.out.println(newName);
//					File f = new File(file,newName);
//					fileItem.write(f);
//					String headersrc = f.getAbsolutePath();
//					UserService service = new UserServiceImpl();
//					user =(User)request.getSession().getAttribute("user");
//					int userid = user.getId();
//;
//					service.uploadHeader(userid,headersrc);
//					System.out.println(headersrc);
//					try {
//						responseJson(response,headersrc);
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//			}
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
//}
